#version 330 core
uniform sampler2D qt_Texture0;
uniform vec4 BackGroundColor;
uniform vec4 BackColor;

uniform vec4 Color_N;
uniform vec4 Color_S;
uniform vec4 Color_W;
uniform vec4 Color_E;

uniform vec4 PointerColor;
uniform vec4 NormalColor;

uniform float FragWidth;
uniform float Direction;

uniform bool PointerMove;

in vec2 qt_TexCoord0;
in vec2 position;

out vec4 FragColor;

float len;
float smowidth;
float halflineW;

float mysmoothstep(float min,float max,float value)
{
    float t = clamp((value - min) / (max - min), 0.0, 1.0);
    return (3.0 - 2.0 * t) * t * t;

    //return t;
    //return sqrt(1-(t-1)*(t-1));
}

vec2 getline(float r)
{
    if(!PointerMove)
        r-=Direction;
    r=radians(r);
    vec2 ret;
    float x=sin(r);
    float y=cos(r);

    if(x==0)
    {
        ret.x=1;
        ret.y=0;
    }
    else
    {
        ret.x=y/x;
        ret.y=-1;
    }
    return ret;
}
//获取点到某个角度线(过原点直线)的距离
float getLineDis(float r,vec2 posi)
{
    //求直线方程（ax+by+c=0的形势）
    //v.x 相当于a
    //v.y 相当于b
    //因为过原点所以c必为0
    vec2 v=getline(r);
    //求点到 直线距离
    return abs(dot(v,position)/sqrt(dot(v,v)));
}

vec2 getPoint(float r)
{
    if(!PointerMove)
        r-=Direction;
    r=radians(r);
    return vec2(sin(r),cos(r));
}

float getDisSmo(float max,float s, float disr)
{
    return 1-smoothstep(max,max+s,disr);
}

void drawSingleLine(float angle,float length, float startr, vec4 colorline)
{
    float dr=(1-step(0.99,distance(position,getPoint(angle))));
    float ds=getDisSmo(halflineW,smowidth, getLineDis(angle,position));
    float smor=getDisSmo(length/2,smowidth,abs(len-length/2-startr+smowidth));
    smor*=dr*ds;
    FragColor=FragColor*(1-smor) + colorline*(smor);
}

void drawDoubleLine(float angle,float length, float startr, vec4 colorline)
{
    float ds=getDisSmo(halflineW,smowidth, getLineDis(angle,position));
    float smor=getDisSmo(length/2,smowidth,abs(len-length/2-startr+smowidth));
    smor*=ds;
    FragColor=FragColor*(1-smor) + colorline*(smor);
}


void main(void)
{
    len= length(position);
    float maxlen=0.99;

    //平滑宽度
    smowidth=FragWidth*2;
    //半线宽
    halflineW=0.003;
//    mat2 ma=mat2(vec2(cos(radians(Direction)),-sin(radians(Direction))),vec2(sin(radians(Direction)),cos(radians(Direction))));
//    vec2 ve= qt_TexCoord0-vec2(0.5);
//    ve=ma*ve+vec2(0.5);
    vec2 ve=vec2(0.5,len*0.1);
    vec4 texturecolor=texture2D(qt_Texture0, ve);

    //表盘边缘平缓
    float smo1= smoothstep(maxlen-smowidth,maxlen,len);
    FragColor=BackGroundColor*smo1+(1-smo1)*(BackColor*texturecolor);

    //线圈
    {
        float smor=getDisSmo(halflineW,halflineW+smowidth/2,abs(len-0.89-halflineW/2+smowidth));
        float smor2=getDisSmo(halflineW,halflineW+smowidth/2,abs(len-0.69+halflineW/2 +smowidth));

//        float smo=0;
//        smo+=getDisSmo(halflineW*4,smowidth, getLineDis(0,position));
//        smo+=getDisSmo(halflineW*4,smowidth, getLineDis(45.,position));
//        smo+=getDisSmo(halflineW*4,smowidth, getLineDis(90.,position));
//        smo+=getDisSmo(halflineW*4,smowidth, getLineDis(135.,position));

//        smor=smor*(1-clamp(smo,0.,1.));
//        smor2=smor2*(1-clamp(smo,0.,1.));

        smor=clamp(smor+smor2,0.,1.);
        FragColor=FragColor*(1-smor) + NormalColor*smor;

    }

    //普通刻度线
    {
        //半径限制
        float smo2=getDisSmo(0.05,smowidth,abs(len-0.94+smowidth));
        //角度直线距离限制
        float smo3=0;
        for(int i=0;i<36;++i)
        {
            if(mod(i,18)<0.5)
                continue;
            //求i*pi/36 角度的直线方程
            vec2 v=getline(i*5);
            //求点到 直线距离
            float disqr=abs(dot(v,position)/sqrt(dot(v,v)));
            float s=getDisSmo(halflineW,smowidth, disqr);
            smo3+=(s*smo2);
        }
        smo3=clamp(smo3,0,1);
        FragColor=FragColor*(1-smo3) + NormalColor*smo3;
    }
    //北
    drawSingleLine(0.,0.1+halflineW/2,0.89-halflineW/2,Color_N);
    //drawSingleLine(0.,0.3,0.39,Color_N);
    //drawSingleLine(0.,0.20,0.51,Color_N);
    //南
    drawSingleLine(180.,0.1+halflineW/2,0.89-halflineW/2,Color_S);
    //drawSingleLine(180.,0.20,0.51,Color_S);
    //西
    drawSingleLine(270.,0.1+halflineW,0.89-halflineW/2,Color_W);
    //drawSingleLine(270.,0.20,0.51,NormalColor);
    //东
    drawSingleLine(90.,0.1+halflineW/2,0.89-halflineW/2,Color_E);
    //drawSingleLine(90.,0.20,0.51,NormalColor);

//    drawDoubleLine(135.,0.20,0.51,NormalColor);
//    drawDoubleLine(45.,0.20,0.51,NormalColor);

//    {
//        float smor=getDisSmo(halflineW,halflineW+smowidth/2,abs(len-0.89-halflineW/2+smowidth));
//        float smor2=getDisSmo(halflineW,halflineW+smowidth/2,abs(len-0.69+halflineW/2 +smowidth));

////        float smo=0;
////        smo+=getDisSmo(halflineW*4,smowidth, getLineDis(0,position));
////        smo+=getDisSmo(halflineW*4,smowidth, getLineDis(45.,position));
////        smo+=getDisSmo(halflineW*4,smowidth, getLineDis(90.,position));
////        smo+=getDisSmo(halflineW*4,smowidth, getLineDis(135.,position));

////        smor=smor*(1-clamp(smo,0.,1.));
////        smor2=smor2*(1-clamp(smo,0.,1.));

//        smor=clamp(smor+smor2,0.,1.);
//        FragColor=FragColor*(1-smor) + NormalColor*smor;

//    }
//    //FragColor=FragColor*texturecolor;
}
